<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <link rel="stylesheet" href="./css/common.css">

    <style>
        .item {
            height: 60px;
            border-bottom: 1px solid #ebebee;
        }
        
        .del {
            right: 0;
            top: 0;
            height: 50px;
            width: 100px;
            background-color: red;
            color: #fff;
        }
        
        img {
            height: 50px;
        }
    </style>
</head>

<body>
    <header>
        <a href="./list.html">列表</a>
        <a href="./add.html">添加</a>
    </header>
    <ul id="list" class="mt-5 flex2">
        <!-- <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <img src="https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg" alt="">
            <div class="fg1 ml-5">
                <span class="f20">张飞</span>
                <p class="f999 f14 mt-5">29岁</p>
            </div>
            <a href="./detail.html">详情</a>
        </li>
        <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <img src="https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg" alt="">
            <div class="fg1 ml-5">
                <span class="f20">张飞</span>
                <p class="f999 f14 mt-5">29岁</p>
            </div>
            <a href="./detail.html">详情</a>
        </li>
        <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
            <img src="https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg" alt="">
            <div class="fg1 ml-5">
                <span class="f20">张飞</span>
                <p class="f999 f14 mt-5">29岁</p>
            </div>
            <a href="./detail.html">详情</a>
        </li> -->
    </ul>


    <script src="https://cdn.bootcdn.net/ajax/libs/zepto/1.2.0/zepto.js"></script>

    <script>
        var obj = {
            getList: function() {
                var self = this;
                $.ajax({
                    url: 'http://localhost:8888/getList',
                    data: {},
                    dataType: 'json',
                    type: 'get',
                    success: function(res) {
                        if (res.code === 666) {
                            self.render(res.data);
                        } else {
                            alert(res.msg);
                        }
                    },
                    error: function() {
                        // todo
                    }
                })
            },

            render: function(list) {
                var str = '';
                list.forEach(function(item) {
                    str += `  <li class="bg-fff item flex jc-sb pl-15 pr-15 aic f16 rel">
                            <img src="https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg" alt="">
                            <div class="fg1 ml-5">
                                <span class="f20">${item.name}</span>
                                <p class="f999 f14 mt-5">${item.age}</p>
                            </div>
                            <a href="./detail.html?_id=${item._id}">详情</a>
                        </li>`;
                })
                $('#list').html(str);
            }
        }

        obj.getList();
    </script>
</body>

</html>